package com.peanut.baby.mvp.subscribe;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.peanut.baby.AppConfig;
import com.peanut.baby.R;
import com.peanut.baby.model.PTag;
import com.peanut.devlibrary.BaseActivity;
import com.peanut.devlibrary.widget.TitleLayout;
import com.tencent.mars.xlog.Log;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TagSubscribeActivity extends BaseActivity
        implements TagSubscribeContract.View,
        TagSubscribeAdapter.onItemCheckChangedListener,
        View.OnClickListener,TitleLayout.OnTitleClickListener{
    private static final String TAG = "TagSubscribeActivity";

    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.tag_layout)
    TagFlowLayout tagLayout;
    @BindView(R.id.subscribe)
    TextView subscribe;

    private TagSubscribePresenter presenter;
    private List<PTag> tags;
    private TagSubscribeAdapter adapter;

    private boolean forceSubscribe = false;
    private boolean requestResult = false;
    public static void start(Context ctx,boolean forceSubscribe){
        ctx.startActivity(new Intent(ctx,TagSubscribeActivity.class).putExtra("force",forceSubscribe));
    }

    public static void startForResult(Activity ctx){
       Intent intent =  new Intent(ctx,TagSubscribeActivity.class);
       intent.putExtra("requestResult",true);
       ctx.startActivityForResult(intent, AppConfig.RequestResult.REQUEST_CODE_SUBSCRIBE);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tag_subscribe);
        ButterKnife.bind(this);
        initView();

        initData();
    }

    private void initView(){
        forceSubscribe = getIntent().getBooleanExtra("force",false);
        requestResult = getIntent().getBooleanExtra("requestResult",false);
        title.setTitleBackVisibility(forceSubscribe ? false:true);
        title.setTitle("标签订阅");
        title.setOnTitleClickedListener(this);

        presenter = new TagSubscribePresenter(this,this);
        tags = new ArrayList<>();
        adapter = new TagSubscribeAdapter(this,tags);
        tagLayout.setAdapter(adapter);
        adapter.setListener(this);
        subscribe.setOnClickListener(this);

    }

    private void initData(){
        presenter.getTags();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            //block key back event
            if(forceSubscribe){
                showForceSubscribeToast();
                return true;
            }else{
                finish();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private void showForceSubscribeToast(){
        showToast("请先选择您要订阅的标签");
    }

    @Override
    public void onTagsGet(boolean success, List<PTag> tags, String failReason) {
        if(success){
            if(tags == null || tags.isEmpty()){
                Log.e(TAG,"failed to get tag list,empty result ");
                showToast("获取标签失败 ");
                finish();
            }else{
                this.tags.addAll(tags);
                adapter.notifyDataChanged();
                if(adapter.hasCheckedItem()){
                    subscribe.setEnabled(true);
                }else{
                    subscribe.setEnabled(false);
                }
            }

        }else{
            Log.e(TAG,"failed to get tag list "+failReason);
            showToast("获取标签失败 "+failReason);
            finish();
        }
    }

    @Override
    public void onSubscribeResult(boolean success, String failReason) {
        dismissProgressDialog();
        if(success){
            if(requestResult){
                setResult(RESULT_OK);
            }
            finish();
        }else{
            showToast("订阅失败,请稍候重试");
        }
    }

    @Override
    public void onItemCheckChanged() {
        if(adapter.hasCheckedItem()){
            subscribe.setEnabled(true);
        }else{
            subscribe.setEnabled(false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.subscribe:
                doSubscribe();
                break;
        }
    }

    private void doSubscribe(){
        List<PTag> tags = adapter.getCheckedList();
        Log.d(TAG,"doSubscribe "+tags.size());
        if(tags == null || tags.isEmpty()){
            showToast("请先选择订阅标签");
            return;
        }
        showProgressDialog("",false);
        presenter.subscribeTags(tags);
    }

    @Override
    public void onTitleBackClicked() {
        finish();
    }

    @Override
    public void onTitleRightClicked() {

    }

    @Override
    public void onTitleClicked() {

    }
}
